﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace WXY.Course.Model
{
    [XmlRoot("Leader")]
    public class LeaderInfo
    {
        public string Name { get; set; }

        public string Image { get; set; }

        public SerializableDictionary MainArchives { get; set; }

        public SerializableDictionary Contributions { get; set; }
    }

    public class SerializableDictionary : Dictionary<string, string>, IXmlSerializable
    {
        #region IXmlSerializable 成员

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(string));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(string));
            bool isEmpty = reader.IsEmptyElement;
            reader.Read();
            if (isEmpty)
                return;
            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                string key = reader.Name;
                reader.ReadStartElement(key);
                string value = reader.ReadContentAsString();
                reader.ReadEndElement();
                this.Add(key, value);
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(string));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(string));
            foreach (string key in this.Keys)
            {
                string keyChar = key.ToString();
                writer.WriteStartElement(keyChar);
                string value = this[key];
                writer.WriteCData(value.ToString());
                writer.WriteEndElement();
            }
        }

        #endregion
    }
}
